<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\AppException;
use App\Models\Config;
use App\Models\Data;
use App\Models\Hospital;
use App\Models\Log;
use App\Models\Member;
use App\Models\MemberAnswer;
use App\Models\Problem;
use App\Models\QuestionMember;
use App\Models\Vote;
use App\Models\VoteT;
use App\Services\ExcelExportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class MemberController extends BaseController
{

    /**
     *
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        $build = Member::query();
        $pageSize = $request->input('pageSize', 50);
        $data = $build->paginate($pageSize);
        return $this->success($data);
    }

    public function destroy($memberId, Request $request)
    {
        $memberData = Member::query()->where('member_id', $memberId)->first();

        if (!$memberData) {
            throw new AppException('find_member_is_fail');
        }

        $result = $memberData->delete();
        return $this->success($result, '删除成功');
    }

    /**
     * 新增数据data
     * @param Request $request
     * @return array
     */
    public function create(Request $request)
    {
        $params = $request->all();
        $info['origin_ym'] = $params['origin_ym'];
        $info['sun_ym'] = $params['sun_ym'];
        $result = Member::query()->create($info);
        return $this->success($result);
    }

    public function info($memberId)
    {
        $memberData = Member::query()->where('member_id', $memberId)->first();
        if (!$memberData) {
            throw new AppException('find_member_is_fail');
        }
        return $this->success($memberData, '获取成功');
    }

    public function editMember($memberId, Request $request)
    {
        $params = $request->all();

        $memberData = Member::query()->where('member_id', $memberId)->first();
        if (!$memberData) {
            throw new AppException('find_member_is_fail');
        }
        $result = Member::query()->where('member_id', $memberId)->update(['origin_ym' => $params['origin_ym'], 'sun_ym' => $params['sun_ym']]);
        return $this->success($memberData, '获取成功');
    }


    public function addErrorLog(Request $request)
    {
        $params = $request->all();

        Log::query()->create([
            'sun_ym' => $params['code'],
            'origin_ym' => $params['ip'],
        ]);
//        $memberData = Member::query()->whereIn('member_id', explode(',', $params['member_id']))->get()->toArray();
//        foreach ($memberData as $key => $value) {
//            Log::query()->create([
//                'member_id' => $value['member_id'],
//                'origin_ym' => $value['origin_ym'],
//                'sun_ym' => $value['sun_ym'],
//                'ip' => $params['ip'],
//            ]);
//        }
        return $this->success([], '错误日志新增成功');

    }

    public function indexErrorLog(Request $request)
    {
        $build = Log::query();
        $pageSize = $request->input('pageSize', 50);
        $data = $build->paginate($pageSize);
        return $this->success($data);
    }


}
